package com.banksoft.admin.controller;

import com.banksoft.admin.dao.AdvertDao;
import com.banksoft.admin.dao.TxApplyDao;
import com.banksoft.admin.entity.TxApply;
import com.banksoft.admin.service.BaseService;
import com.banksoft.admin.service.TxApplyService;
import com.banksoft.common.BaseController;
import com.banksoft.common.PageInfo;
import com.banksoft.util.DateUtil;
import com.banksoft.util.StringNumber;
import com.banksoft.util.StringUtil;
import com.banksoft.util.UUIDFactory;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/5.
 */
@Controller
@RequestMapping("/txApply")
public class TxApplyController extends BaseController {
    @Autowired
    BaseService baseService;
    @Autowired
    TxApplyService txApplyService;

    @RequestMapping("/list")
    public String list(){
        return "tx/tx_list";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public Object listData(String status,String type,String startDate,String endDate,PageInfo pageInfo){

        Map<String,Object> map = new HashMap<>();
        map.put("status",status);
        map.put("applyType",type);
        if(StringUtil.isNotNull(startDate))
            map.put("startDate", DateUtil.getLongFormStrDate(startDate));
        if(StringUtil.isNotNull(endDate))
            map.put("endDate",DateUtil.getLongFormStrDate(endDate));

        pageInfo.setParams(map);

        pageInfo = this.page(TxApplyDao.class,pageInfo);


        return this.getPageResult(pageInfo);
    }


    @RequestMapping("/loadData")
    @ResponseBody
    public JSONObject loadData(String startDate,String endDate,String status,String type){
       try {
           Map<String, Object> map = new HashMap<>();
           map.put("status", status);
           map.put("applyType", type);
           if (StringUtil.isNotNull(startDate))
               map.put("startDate", DateUtil.getLongFormStrDate(startDate));
           if (StringUtil.isNotNull(endDate))
               map.put("endDate", DateUtil.getLongFormStrDate(endDate));

           map.put("type", type);
           List<TxApply> list = this.txApplyService.list(map);
           String ljtxje = "0";
           int ljtxbs = 0;
           if (StringUtil.isNotEmpty(list)) {
               for (TxApply dto : list) {
                   ljtxje = StringNumber.add(dto.getAmount(), ljtxje);
               }
               ljtxbs = list.size();
           }
           String sDate = DateUtil.getFormatDate(new Date(), "yyyy-MM-dd") + " 00:00:00";
           String eDate = DateUtil.getFormatDate(new Date(), "yyyy-MM-dd") + " 23:59:59";
           map.put("startDate", DateUtil.getLongFormStrDate(sDate));
           map.put("endDate", DateUtil.getLongFormStrDate(eDate));
           map.put("status", "1");
           List<TxApply> jrlist = this.txApplyService.list(map);
           String jrtxje = "0";
           int jrtxbs = 0;
           if (StringUtil.isNotEmpty(jrlist)) {
               for (TxApply dto : jrlist) {
                   jrtxje = StringNumber.add(dto.getAmount(), jrtxje);
               }
               jrtxbs = jrlist.size();
           }
           Map result = new HashMap();
           result.put("ljtxje", ljtxje);
           result.put("ljtxbs", ljtxbs);
           result.put("jrtxje", jrtxje);
           result.put("jrtxbs", jrtxbs);
           return this.FmtResult(true,null,result);
       }catch (Exception e){
           e.printStackTrace();
       }
       return this.FmtResult(false,null,null);
    }

    @RequestMapping("/beforeEdit")
    public String beforeEdit(String id,Model model){
        if(StringUtil.isNotNull(id)){
            TxApply apply = this.baseService.findById(TxApplyDao.class,id);
            model.addAttribute("data",apply);
        }
        return "tx/tx_edit";
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object save(String id,String status,String bz,HttpServletRequest request){
        try {
            JSONObject json = this.txApplyService.verify(id,status,bz,request);
            return json;
        }catch (Exception e){
            if(e instanceof NullPointerException){
               return this.FmtResult(false,"审核失败:"+e.getMessage(),null);
            }
            e.printStackTrace();
            return this.FmtResult(false,"审核失败",null);
        }

    }

}
